package com.lovo.cloud.service.impl;

import com.lovo.cloud.model.dos.OrderDO;
import com.lovo.cloud.dao.OrderDAO;
import com.lovo.cloud.model.dto.OrderDTO;
import com.lovo.cloud.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luo
 * @since 2020-12-04
 */
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderDAO, OrderDO> implements OrderService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private OrderDAO orderDAO;

    RabbitTemplate.ConfirmCallback callback=(CorrelationData correlationData, boolean b, String s)->{
        //获得MQ返回的唯一标志orderNum
        String orderNum=  correlationData.getId();
        this.updateOrder(orderNum,b);
    };

    private boolean updateOrder(String orderNum,boolean bl) {

        boolean flag = false;
        int num = orderDAO.updateTagByOrderNum(orderNum,bl);
        if (num>0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean saveOrder(OrderDO orderDO) {

        boolean bl = false;
        orderDO.setOrderNum(System.currentTimeMillis()+"");
        orderDO.setOrderCreatetime(Timestamp.valueOf(LocalDateTime.now()));
        orderDO.setOrderTag(1);
        int i = orderDAO.insert(orderDO);
        if (i>0) {
//            MovieDO movieDO = new MovieDO();
//            movieDO.setMovieName(orderDO.getMovieName());
//            movieDO.setMovieNumber(orderDO.getMovieNum());
//            movieDO.setMoviePrice(orderDO.getMoviePrice());

//            MovieOrderDTO dto = new MovieOrderDTO();
//            dto.setOrderNum(orderDO.getOrderNum());
//            dto.setMovieName(orderDO.getMovieName());
//            dto.setMovieNum(orderDO.getMovieNum());

            OrderDTO orderDTO = new OrderDTO();
            orderDTO.setMovieName(orderDO.getMovieName());
            orderDTO.setMovieNum(orderDO.getMovieNum());
            orderDTO.setOrderNum(orderDO.getOrderNum());

            //开启手动确认
            rabbitTemplate.setMandatory(true);//开启消息确认
            //绑定回调函数
            rabbitTemplate.setConfirmCallback(callback);
            //组装关联数据
            CorrelationData correlationData=new CorrelationData(orderDO.getOrderNum());
            //发送到MQ
            rabbitTemplate.convertAndSend("directExchange","order",orderDTO,correlationData);
            bl = true;
        }

        return bl;
    }

    @Override
    public OrderDO getOrderByNum(String orderNum) {
        return null;
    }

    @Override
    public boolean updateTag(String orderNum,Boolean type) {
        boolean bl = false;
        int i = orderDAO.updateOrderTag(orderNum, type);
        if (i>0) {
            bl = true;
        }
        return bl;
    }


}
